//
//  WBNetworkManager+Extension.swift
//  weibo
//
//  Created by 林帆 on 2017/4/12.
//  Copyright © 2017年 林帆. All rights reserved.
//

import Foundation

// MARK : - 封装新浪微博的网络
extension WBNetworkManager {
    
    func statusList(since_id : Int64 , max_id : Int64 ,  completion : @escaping ( _ list : [[String : AnyObject]], _ isSuccess : Bool ) -> ( ) ) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["since_id": "\(since_id)",
            "max_id": "\(max_id > 0 ? max_id - 1 : 0)"]
        
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            //   print(json ?? "是吗")
            //   print("woyun \(String(describing: json))")
            
            // 从json 中获取statuses 字典数组
            // 如果 as？失败，result = nil
            let jsonDic = json as? NSDictionary
            let result =  jsonDic?["statuses"] as? [[String : AnyObject]]
            
            completion(result! , isSuccess)

        }
    }
    
    func unreadCount(completion: @escaping (_ count: Int)->( )) {
        guard let uid = uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/comments/to_me.json"
        
        let params = ["uid" : uid]
        
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            let dict = json as? [String: AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 2 )
//            print(json ?? "" )
        }
    }
}

